//
//  HPTransRecordView.m
//  HPZZTong
//
//  Created by handpay on 14-4-10.
//  Copyright (c) 2014年 Handpay. All rights reserved.
//

#import "HPTransRecordView.h"
#import "ZZTongDefine.h"
#import "HPDateTimeListView.h"
#import "HPUIEngine.h"
#import "ZZTongClientEngine.h"
#import "HPClientEngine.h"

#define PAGESIZE   @"5"

#define RecordCellCount  10
#define RecordCellCountTransfer  6
#define RecordCellHeight  35

#define kCellDesLabelTag         10001
#define kCellValueLabelTag       10002

#define kImageViewBgTag          1001
//#define kCellLineTag             10003

@interface HPTransRecordView (){
    
    int currentPage;
    int totalPage;
    
    UIScrollView* contentScrolView;
    UIView* bottomView;
    UILabel* pageLabel;
    
    UIButton* homePageButton;
    UIButton* prevPageButton;
    UIButton* nextPageButton;
    UIButton* lastPageButton;
    
    UIView* dateBgView;
    HPDateTimeListView* endDateView;
    HPDateTimeListView* beginDateView;
    
    UIButton* queryButton;
    UITableView* recordTableView;
    
    UILabel* totalAmountLabel;
    UILabel* totalNumLabel;
    
    NSArray* dataArray;
    NSArray* cellCountArray;
    
    NSUInteger selectId;
    BOOL isUpdate;
}
@property(nonatomic,retain)NSString* totalAmount;
@property(nonatomic,retain)NSString* totalNum;

@end

@implementation HPTransRecordView
@synthesize appCode;

- (id)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        currentPage = 0;
        totalPage = 0;
        selectId = 0;
        [self createView];
        [self treatBtn];
    }
    return self;
}

- (void)createBottomView:(int)top{
    
    CGRect frame = contentScrolView.frame;
    int h = 70;
    int left = 10;
    int w = frame.size.width - 2 * left;
    
    bottomView = [[UIView alloc]initWithFrame:CGRectMake(left, top, w, h)];
    bottomView.backgroundColor = [UIColor clearColor];
    [contentScrolView addSubview:bottomView];
    
    h = 21;
    pageLabel = [[UILabel alloc]initWithFrame:CGRectMake(left, 0, w, h)];
    pageLabel.textColor = kNormalFontColor;
    pageLabel.font = kNormalFont;
    pageLabel.text = @"第0页  共0页";
    pageLabel.textAlignment = NSTextAlignmentCenter;
    [bottomView addSubview:pageLabel];

    
    UIImage* normalImage = [[UIImage imageNamed:@"bluebutton.png"]stretchableImageWithLeftCapWidth:9 topCapHeight:0];
    UIImage* hoverImage = [[UIImage imageNamed:@"bluebuttonhover.png"]stretchableImageWithLeftCapWidth:9 topCapHeight:0];
    
    top = 30;
    left = 8;
    w = 65;
    h = 40;
    //首页
    homePageButton = [[UIButton alloc]initWithFrame:CGRectMake(left, top, w, h)];
    [homePageButton setBackgroundImage:normalImage forState:UIControlStateNormal];
    [homePageButton setBackgroundImage:hoverImage forState:UIControlStateHighlighted];
    [homePageButton setTitle:@"首页" forState:UIControlStateNormal];
    [homePageButton setTitle:@"首页" forState:UIControlStateHighlighted];
    [homePageButton addTarget:self action:@selector(homePageClick) forControlEvents:UIControlEventTouchUpInside];
    
    left += w + 8;
    //上一页
    prevPageButton = [[UIButton alloc]initWithFrame:CGRectMake(left, top, w, h)];
    [prevPageButton setBackgroundImage:normalImage forState:UIControlStateNormal];
    [prevPageButton setBackgroundImage:hoverImage forState:UIControlStateHighlighted];
    [prevPageButton setTitle:@"上一页" forState:UIControlStateNormal];
    [prevPageButton setTitle:@"上一页" forState:UIControlStateHighlighted];
    [prevPageButton addTarget:self action:@selector(prevPageClick) forControlEvents:UIControlEventTouchUpInside];
    
    left += w + 8;
    //下一页
    nextPageButton = [[UIButton alloc]initWithFrame:CGRectMake(left, top, w, h)];
    [nextPageButton setBackgroundImage:normalImage forState:UIControlStateNormal];
    [nextPageButton setBackgroundImage:hoverImage forState:UIControlStateHighlighted];
    [nextPageButton setTitle:@"下一页" forState:UIControlStateNormal];
    [nextPageButton setTitle:@"下一页" forState:UIControlStateHighlighted];
    [nextPageButton addTarget:self action:@selector(nextPageClick) forControlEvents:UIControlEventTouchUpInside];
    
    left += w + 8;
    //末页
    lastPageButton = [[UIButton alloc]initWithFrame:CGRectMake(left, top, w, h)];
    [lastPageButton setBackgroundImage:normalImage forState:UIControlStateNormal];
    [lastPageButton setBackgroundImage:hoverImage forState:UIControlStateHighlighted];
    [lastPageButton setTitle:@"末页" forState:UIControlStateNormal];
    [lastPageButton setTitle:@"末页" forState:UIControlStateHighlighted];
    [lastPageButton addTarget:self action:@selector(lastPageClick) forControlEvents:UIControlEventTouchUpInside];
    
    [bottomView addSubview:homePageButton];
    [bottomView addSubview:prevPageButton];
    [bottomView addSubview:nextPageButton];
    [bottomView addSubview:lastPageButton];
}
- (void)createView{
    CGRect tempFrame = CGRectMake(10, 206, 300, 180);
    recordTableView.frame = tempFrame;
    if (HP_Device_iPhone5) {
        tempFrame.size.height += IPHONE5_ADD_HEIGHT;
        recordTableView.frame = tempFrame;
    }
    [self createScrollView];
    [self createBottomView:0];
}
- (void)responseCallBackSuccess:(NSString *)action withData:(NSDictionary *)respData{
    if ([action isEqualToString:NET_ACTION_QUERY_BILLS]) {
        currentPage = [[respData objectForKey:@"pageNo"] intValue];
        totalPage = [[respData objectForKey:@"totalPages"] intValue];
        self.totalAmount = [Utility getFormatMoneyAndSymbol:[respData objectForKey:@"amounts"]];
        
        NSArray* arr = [respData objectForKey:@"list"];
        if (arr && arr.count >0) {
            [self caculateCellCount:arr];
            dataArray = [[NSArray alloc]initWithArray:arr];
            self.totalNum = [NSString stringWithFormat:@"%d",(int)arr.count];
        }else{
            dataArray = [[NSArray alloc]init];
        }
        
        [HPUIENG removeWaiting];
        [self performSelectorOnMainThread:@selector(refreshUI:) withObject:respData waitUntilDone:YES];
    }
}
- (void)caculateCellCount:(NSArray*)array{
    if (!array || array.count ==0) {
        return;
    }
    UILabel* label = [[UILabel alloc] initWithFrame:CGRectMake(100, 8, 190, 30)];

    [label setFont:kNormalFont];
    
    NSMutableArray* cellCountA = [[NSMutableArray alloc]initWithCapacity:10];
    int cellCount = RecordCellCount;
    NSUInteger cellH;
    NSNumber* countNumber = [NSNumber numberWithInt:cellCount];
    NSNumber* hNumber;
    NSUInteger imageH = (RecordCellCount - 1) * RecordCellHeight;
    for (NSDictionary* recordDic in array) {
        NSString* status = [recordDic objectForKey:@"status"];
        
        NSArray* tArray;
        if ([status isEqualToString:@"1"]) {
            countNumber = [NSNumber numberWithInt:cellCount - 1];
            tArray = @[countNumber,[NSNumber numberWithUnsignedInteger:imageH]];
        }else{
            countNumber = [NSNumber numberWithInt:cellCount];
            NSString* transStatus = [recordDic objectForKey:@"transStatus"];
            label.text = transStatus;
        
            if ([status isEqualToString:@"2"]) {
                label.frame = CGRectMake(10, 8, 280, 30);
            }
            
            cellH = [self heightWithLabel:label];
            hNumber = [NSNumber numberWithUnsignedInteger:cellH];
            NSUInteger imgH = imageH + cellH;
            tArray = @[countNumber,[NSNumber numberWithUnsignedInteger:imgH],hNumber];
        }
        if (tArray) {
            [cellCountA addObject:tArray];
        }
    }
    [label release];
    cellCountArray = [[NSArray alloc]initWithArray:cellCountA];
    [cellCountA release];
}
- (void)createDateView:(CGRect)frame_{
    int left = 10;
    int top = 0;
    int space = 10;
    
    dateBgView = [[UIView alloc]initWithFrame:frame_];
    dateBgView.backgroundColor = [UIColor whiteColor];
    dateBgView.layer.cornerRadius = 8.0;
    dateBgView.layer.borderColor = kNormalFontColor.CGColor;
    dateBgView.layer.borderWidth = 1;
    [contentScrolView addSubview:dateBgView];
    
    int h = frame_.size.height / 2 - 1;
    int w = 80;
    UILabel* beginLabel = [[UILabel alloc]initWithFrame:CGRectMake(left, top, w, h)];
    beginLabel.text = @"起始日期：";
    beginLabel.textColor = kNormalFontColor;
    beginLabel.font = kNormalFont;
    [dateBgView addSubview:beginLabel];
    [beginLabel release];
    
    UILabel* endLabel = [[UILabel alloc]initWithFrame:CGRectMake(left, top, w, h)];
    endLabel.text = @"结束日期：";
    endLabel.textColor = kNormalFontColor;
    endLabel.font = kNormalFont;
    [dateBgView addSubview:endLabel];
    
    left += w + space;
    w = 170;
    beginDateView = [[HPDateTimeListView alloc]initWithFrame:CGRectMake(left, top, w, h) label:[Utility getNowDate] mode:HPDateTimeListViewModeDate];
    beginDateView.backgroundColor = [UIColor clearColor];
    [dateBgView addSubview:beginDateView];
    
    endDateView = [[HPDateTimeListView alloc]initWithFrame:CGRectMake(left, top, w, h) label:[Utility getNowDate] mode:HPDateTimeListViewModeDate];
    endDateView.backgroundColor = [UIColor clearColor];
    [dateBgView addSubview:endDateView];
    
    
    left += 160;
    w = 8;
    h = 13;
    UIImage* arrowImage = [UIImage imageNamed:@"arrow.png"];
    UIImageView* beginImageView = [[UIImageView alloc]initWithFrame:CGRectMake(left, (frame_.size.height / 2 - h) /2, w, h)];
    beginImageView.image = arrowImage;
    [dateBgView addSubview:beginImageView];
    [beginImageView release];
    
    UIImageView* endImageView = [[UIImageView alloc]initWithFrame:CGRectMake(left, top, w, h)];
    endImageView.image = arrowImage;
    [dateBgView addSubview:endImageView];
    
    
    left = 10;
    top += frame_.size.height / 2;
    h = 1;
    w = dateBgView.frame.size.width - 2 * left;
    UIView* lineView = [[UIView alloc]initWithFrame:CGRectMake(left, top, w, h)];
    lineView.backgroundColor = kLineFontColor;
    [dateBgView addSubview:lineView];
    [lineView release];
    
    top += h;
    CGRect tempframe = endDateView.frame;
    tempframe.origin.y = top;
    endDateView.frame = tempframe;
    
    tempframe = endLabel.frame;
    tempframe.origin.y = top;
    endLabel.frame = tempframe;
    [endLabel release];
    
    top += (frame_.size.height / 2 - 13) / 2;
    tempframe = endImageView.frame;
    tempframe.origin.y = top;
    endImageView.frame = tempframe;
    [endImageView release];
}
- (void)createTotalView:(CGRect)frame_{
    UIView* bgView = [[UIView alloc]initWithFrame:frame_];
    bgView.backgroundColor = [UIColor clearColor];
    
    int left = 0;
    int top = 0;
    int h = frame_.size.height;
    int w = 50;
    UILabel* tAmountL= [[UILabel alloc]initWithFrame:CGRectMake(left, top, w, h)];
    tAmountL.textColor = kNormalFontColor;
    tAmountL.font = kNormalFont;
    tAmountL.text = @"总金额";
    [bgView addSubview:tAmountL];
    [tAmountL release];
    
    left += w + 5;
    w = 110;
    totalAmountLabel = [[UILabel alloc]initWithFrame:CGRectMake(left, top, w, h)];
    totalAmountLabel.textColor = kTitleFontColor;
    totalAmountLabel.font = kNormalFont;
    totalAmountLabel.text = @"0";
    [bgView addSubview:totalAmountLabel];
    
    left += w + 8;
    w =  57;
    UILabel* tNumLabel = [[UILabel alloc]initWithFrame:CGRectMake(left, top, w, h)];
    tNumLabel.textColor = kNormalFontColor;
    tNumLabel.font = kNormalFont;
    tNumLabel.text = @"总笔数";
    [bgView addSubview:tNumLabel];
    [tNumLabel release];

    left += w + 4;
    w =  50;
    totalNumLabel = [[UILabel alloc]initWithFrame:CGRectMake(left, top, w, h)];
    totalNumLabel.textColor = kTitleFontColor;
    totalNumLabel.font = kNormalFont;
    totalNumLabel.text = @"0";
    [bgView addSubview:totalNumLabel];
    
    [contentScrolView addSubview:bgView];
    [bgView release];
}
- (void)createScrollView{
    NSUInteger h = self.frame.size.height - 10;
    int left = 0;
    int w = self.frame.size.width;
    int top = 0;
    int space = 10;
    contentScrolView = [[UIScrollView alloc]initWithFrame:CGRectMake(left, top, w, h)];
    contentScrolView.backgroundColor = [UIColor clearColor];
    [self addSubview:contentScrolView];
    
    left = 10;
    top = left;
    h = 96;
    w = w - 2 * left;
    [self createDateView:CGRectMake(left, top, w, h)];
    
    top += h + space;
    h = 45;
    UIImage* normalImage = [[UIImage imageNamed:@"bluebutton.png"]stretchableImageWithLeftCapWidth:9 topCapHeight:0];
    UIImage* hoverImage = [[UIImage imageNamed:@"bluebuttonhover.png"]stretchableImageWithLeftCapWidth:9 topCapHeight:0];
    
    queryButton = [[UIButton alloc]initWithFrame:CGRectMake(left, top, w, h)];
    [queryButton setBackgroundImage:normalImage forState:UIControlStateNormal];
    [queryButton setBackgroundImage:hoverImage forState:UIControlStateHighlighted];
    [queryButton setTitle:@"查询" forState:UIControlStateNormal];
    [queryButton setTitle:@"查询" forState:UIControlStateHighlighted];
    [queryButton addTarget:self action:@selector(queryRecord) forControlEvents:UIControlEventTouchUpInside];
    [contentScrolView addSubview:queryButton];
    
    top += h + space;
    h = 22;
    [self createTotalView:CGRectMake(left, top, w, h)];
    
    top += h + space;
    h = 220;
    recordTableView = [[UITableView alloc]initWithFrame:CGRectMake(left, top, w, h) style:UITableViewStylePlain];
    recordTableView.backgroundColor = [UIColor clearColor];
    recordTableView.dataSource = self;
    recordTableView.delegate = self;
    recordTableView.hidden = YES;
    recordTableView.userInteractionEnabled = NO;
    [contentScrolView addSubview:recordTableView];
}
- (void)queryRecord {
#if ZZT_FOR_TEST

    NSDictionary* dic = @{@"pageNo": @"1",
                          @"totalPages":@"3",
                          @"amounts" : @"¥2949024",
                          @"items":@"34",
                          @"list": @[@{@"appcode" : @"1",
                                       @"transTime" : @"20140201111111",
                                       @"transStatus" : @"绝对是发生",
                                       @"status" : @"放手放开了舒服",
                                       @"transSN" : @"923232323",
                                       @"cardNoIn" : @"6221111122222222",
                                       @"cardNoOut" : @"6221111122222222",
                                       @"amount" : @"19000",
                                       @"fee" : @"400",
                                       @"total" : @"19400",
                                       @"imageUrl" : @"www.baidu.com",
                                       @"DetailUrl" : @"http://10.48.193.230/hpay/hpaysft/images/voucher/HPZZT/20140411/bill/227371_6.jpeg"},
                                     
                                     @{@"appcode" : @"1",
                                       @"transTime" : @"20150201111111",
                                       @"transStatus" : @"绝对是发生",
                                       @"status" : @"放手放开了舒服",
                                       @"transSN" : @"923232323",
                                       @"cardNoIn" : @"6221111122222222",
                                       @"cardNoOut" : @"6221111122222222",
                                       @"amount" : @"19000",
                                       @"fee" : @"400",
                                       @"total" : @"19400",
                                       @"imageUrl" : @"www.baidu.com",
                                       @"DetailUrl" : @"www.baidu.com"},
                                     
                                     @{@"appcode" : @"1",
                                       @"transTime" : @"20160201111111",
                                       @"transStatus" : @"绝对是发生",
                                       @"status" : @"放手放开了舒服",
                                       @"transSN" : @"923232323",
                                       @"cardNoIn" : @"6221111122222222",
                                       @"cardNoOut" : @"6221111122222222",
                                       @"amount" : @"19000",
                                       @"fee" : @"400",
                                       @"total" : @"19400",
                                       @"imageUrl" : @"www.baidu.com",
                                       @"DetailUrl" : @"www.baidu.com"},
                                     
                                     @{@"appcode" : @"1",
                                       @"transTime" : @"20170201111111",
                                       @"transStatus" : @"绝对是发生",
                                       @"status" : @"放手放开了舒服",
                                       @"transSN" : @"923232323",
                                       @"cardNoIn" : @"6221111122222222",
                                       @"cardNoOut" : @"6221111122222222",
                                       @"amount" : @"19000",
                                       @"fee" : @"400",
                                       @"total" : @"19400",
                                       @"imageUrl" : @"www.baidu.com",
                                       @"DetailUrl" : @"www.baidu.com"},
                                     
                                     @{@"appcode" : @"1",
                                       @"transTime" : @"20180201111111",
                                       @"transStatus" : @"绝对是发生",
                                       @"status" : @"放手放开了舒服",
                                       @"transSN" : @"923232323",
                                       @"cardNoIn" : @"6221111122222222",
                                       @"cardNoOut" : @"6221111122222222",
                                       @"amount" : @"19000",
                                       @"fee" : @"400",
                                       @"total" : @"19400",
                                       @"imageUrl" : @"www.baidu.com",
                                       @"DetailUrl" : @"www.baidu.com"},
                                     
                                     @{@"appcode" : @"1",
                                       @"transTime" : @"20190201111111",
                                       @"transStatus" : @"绝对是发生",
                                       @"status" : @"放手放开了舒服",
                                       @"transSN" : @"923232323",
                                       @"cardNoIn" : @"6221111122222222",
                                       @"cardNoOut" : @"6221111122222222",
                                       @"amount" : @"19000",
                                       @"fee" : @"400",
                                       @"total" : @"19400",
                                       @"imageUrl" : @"www.baidu.com",
                                       @"DetailUrl" : @"www.baidu.com"}]};
    [self responseCallBackSuccess:NET_ACTION_QUERY_BILLS withData:dic];
#else
    [self sendRecordRequest:1];
#endif
}
- (void)updateRecord{
    if (isUpdate) {
        [self sendRecordRequest:currentPage];
    }
}

- (void)homePageClick {
    [self sendRecordRequest:1];
}

- (void)prevPageClick {
    [self sendRecordRequest:(currentPage - 1)];
}

- (void)nextPageClick {
    [self sendRecordRequest:(currentPage + 1)];
}

- (void)lastPageClick {
    [self sendRecordRequest:totalPage];
}

- (void)sendRecordRequest:(int)pageIndex{
    NSString* startTime = beginDateView.value;
    NSString* endTime = endDateView.value;
    
    if (![Utility isLegalDate:startTime] || ![Utility isLegalDate:endTime]) {
        [HPUIENG showAlert:@"当前日期查询不到所需信息"];
        return;
    }
    if ([startTime compare:endTime] == NSOrderedDescending) {
        [HPUIENG showAlert:@"结束日期不能小于起始日期"];
        return;
    }
    startTime = [NSString stringWithFormat:@"%@000000", startTime];
    endTime = [NSString stringWithFormat:@"%@235959", endTime];
    
    ZZTongClientEngine* zztEngine = [ZZTongClientEngine sharedInstance];
    NSDictionary* params = @{@"startTime": startTime,
                             @"endTime": endTime,
                             @"pageNo": [NSString stringWithFormat:@"%d", pageIndex],
                             @"pageSize": PAGESIZE,
//                             KEY_CSN: DES_ENC(SWIPERENG.csn),
                             KEY_CHANNEL: [zztEngine getChannel],
                             KEY_ACCOUNT:DES_ENC(ZZT_CLIENG.accountInfo.account),
                             @"appCode":self.appCode};
    
    [zztEngine.netEngine sendRequest:NET_ACTION_QUERY_BILLS withParams:params withDelegate:self];
    [zztEngine.uiEngine showNetWaiting];
}
- (void)treatBtn{
    if (totalPage < 2) { // 只有一页或者没有数据，按钮不显示
        bottomView.hidden = YES;
    }else{
        bottomView.hidden = NO;
        prevPageButton.enabled = homePageButton.enabled = (currentPage != 1);
        nextPageButton.enabled = lastPageButton.enabled = (currentPage != totalPage);
        
        CGSize size = contentScrolView.contentSize;
        CGRect frame = bottomView.frame;
        frame.origin.y = size.height;
        bottomView.frame = frame;
        
        size.height += bottomView.frame.size.height + 10;
        contentScrolView.contentSize = size;
    }
}

- (void)refreshUI:(NSDictionary*)resp{
    totalNumLabel.text = [resp objectForKey:@"items"];
    totalAmountLabel.text = self.totalAmount;
    pageLabel.text = [NSString stringWithFormat:@"第%d页  共%d页", currentPage, totalPage];
    
    recordTableView.hidden = NO;
    if (totalPage > 0) {
        CGRect tempFrame = recordTableView.frame;
        CGSize contentSize = contentScrolView.frame.size;
        
        int count = 0;
        if (totalPage == 1) {
            count = [self.totalNum intValue];
        }else{
            if (currentPage == totalPage) {
                count = [self.totalNum intValue] % [PAGESIZE intValue];
                if (count == 0) {
                    count = PAGESIZE.intValue;
                }
            }else{
                count = [PAGESIZE intValue];
            }
        }
        
        int imageHeight = RecordCellCount * RecordCellHeight;
        if ([appCode isEqualToString:@"1"]) {//收款
            imageHeight = RecordCellCountTransfer * RecordCellHeight;
        }
        int totalH = 0;
        
        for (int i = 0; i < count; i++) {
            if ([appCode isEqualToString:@"3"]) {
                if (cellCountArray) {
                    NSArray* arr = [cellCountArray objectAtIndex:i];
                    if (arr) {
                        imageHeight = ((NSNumber*)[arr objectAtIndex:1]).intValue;
                    }
                }
            }
            UIView* tagView = [contentScrolView viewWithTag:(kImageViewBgTag + i)];
            if (tagView) {
                [tagView removeFromSuperview];
            }

            UIButton* imageView = [[UIButton alloc]initWithFrame:CGRectMake(5, (recordTableView.frame.origin.y + (totalH - 3)), 310, imageHeight - 31)];
            imageView.backgroundColor = [UIColor whiteColor];
            imageView.layer.cornerRadius = 12.0;
            imageView.layer.borderWidth = 3;
            imageView.tag = kImageViewBgTag + i;
            imageView.layer.borderColor = kTitleFontColor.CGColor;
            [imageView addTarget:self action:@selector(recordClick:) forControlEvents:UIControlEventTouchUpInside];
            [contentScrolView addSubview:imageView];
            [contentScrolView sendSubviewToBack:imageView];
            [imageView release];
            
            totalH += imageHeight;
        }
        
        if (count < PAGESIZE.intValue) {
            for (int i = count; i<PAGESIZE.intValue; i++) {
                UIView* tagView = [contentScrolView viewWithTag:(kImageViewBgTag + i)];
                if (tagView) {
                    [tagView removeFromSuperview];
                }
            }
        }
        tempFrame.size.height = totalH - RecordCellHeight;
        contentSize.height = (tempFrame.size.height + 222);
        
        recordTableView.frame = tempFrame;
        contentScrolView.contentSize = contentSize;
        [self treatBtn];
    }else{
        for (int i = 0; i < PAGESIZE.intValue; i++) {
            UIView* tagView = [contentScrolView viewWithTag:(kImageViewBgTag + i)];
            if (tagView) {
                [tagView removeFromSuperview];
            }
        }
        [HPUIENG showAlert:@"暂无交易记录"];
        recordTableView.hidden = YES;
        
        contentScrolView.contentSize = contentScrolView.frame.size;
    }
    
    [recordTableView reloadData];
}
- (void)downloadImage:(NSString*)imageUrl{
    [ZZT_UIENG showNetWaiting];
    dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
        UIImage* image =  [UIImage imageWithData:[NSData dataWithContentsOfURL:[NSURL URLWithString:imageUrl]]];
        [HPUIENG removeWaiting];
        if (image) {
            NSDictionary* dic = @{kSignImage: image,
                                  kBusinissType: transRecord};
            [HPUIENG showScreen:SCR_SIGN_TRANSACTION params:dic];
        }else{
            NSDictionary* recordDic = [dataArray objectAtIndex:(selectId)];
            [self showTransferInfo:recordDic];
            
        }

    });
    
    }
- (void)showFastTransferInfo:(NSDictionary*)recordDic{
    NSString* transTime = [Utility getFormatDate:[recordDic objectForKey:@"transTime"]];
    NSString* transSN = [recordDic objectForKey:@"transSN"];
    NSString* transStatus = [recordDic objectForKey:@"transStatus"];
    NSString* status = [recordDic objectForKey:@"status"];
    NSString* cardNoOut = [Utility encryptCardID:[recordDic objectForKey:@"cardNoOut"]];
    NSString* amount = [Utility getFormatMoneyAndSymbol:[recordDic objectForKey:@"amount"]];
    NSString* cardNoIn = [Utility encryptCardID:[recordDic objectForKey:@"cardNoIn"]];
    NSString* fee_ = [Utility getFormatMoneyAndSymbol:[recordDic objectForKey:KEY_FEE]];
    NSString* total = [Utility getFormatMoneyAndSymbol:[recordDic objectForKey:KEY_TOTAL]];

    if ([status isEqualToString:@"1"]) {
        NSArray*  aboveData = @[@[@"交易金额", total],
                                @[@"转出卡号", cardNoOut],
                                @[@"交易状态", @"交易成功"],
                                @[@"交易时间", transTime],
                                @[@"交易流水号", transSN]];
        
        NSArray* belowData = @[@[@"交易类型",   @"快速转账"],
                               @[@"收款人账号", cardNoIn],
                               @[@"转账金额", amount],
                               @[@"手续费", fee_]];
        NSDictionary* dic = @{kBelowData: belowData,
                              kAboveData: aboveData};
        [HPUIENG showScreen:SCR_TRANSACTION_SUCCESS params:dic];
    }else{
        NSString* titleDetail = @"交易失败";
        NSString* statusTips = @"失败原因";
        if ([status isEqualToString:@"2"]) {
            titleDetail = @"交易处理中";
            statusTips = @"";
        }
        NSArray*  aboveData = @[@[@"交易金额", total],
                                @[@"转出卡号", cardNoOut],
                                @[@"交易状态", titleDetail],
                                @[@"交易时间", transTime],
                                @[@"交易流水号", transSN],
                                @[@"交易类型",   @"快速转账"],
                                @[@"收款人账号", cardNoIn],];
        
        NSArray* belowData = @[@[statusTips, transStatus, @"3"]];
        NSDictionary* dic = @{kTransType: transTypeFastTransfer,
                              kAboveData: aboveData,
                              kBelowData: belowData,
                              @"status": status};
        [HPUIENG showScreen:SCR_TRANSACTION_FAIL params:dic];
    }
}
- (void)showFailDetail:(NSDictionary*)recordDic{
    NSString* transTime = [Utility getFormatDate:[recordDic objectForKey:@"transTime"]];
    NSString* transSN = [recordDic objectForKey:@"transSN"];
    NSString* transStatus = [recordDic objectForKey:@"transStatus"];
    
    NSString* cardNoOut = [Utility encryptCardID:[recordDic objectForKey:@"cardNoOut"]];
    NSString* amount = [Utility getFormatMoneyAndSymbol:[recordDic objectForKey:@"amount"]];
    
    NSString* tranMediaType = [recordDic objectForKey:@"tranMediaType"];
    if (![NSString isNilOrEmpty:tranMediaType]) {
        cardNoOut = [[cardNoOut stringByAppendingString:@" /"]stringByAppendingString:tranMediaType];
    }
    
    NSArray*  aboveData = @[@[@"交易类型", @"消费/SALE"],
                            @[@"交易流水号",transSN],
                            @[@"交易时间", transTime],
                            @[@"消费卡号", cardNoOut],
                            @[@"消费金额", amount]];
    
    NSString* status = [recordDic objectForKey:@"status"];
    
    NSArray* belowData = nil;
    
    if ([status isEqualToString:@"0"]) {
        belowData = @[@[@"失败原因", transStatus, @"3"]];
        
    }else if([status isEqualToString:@"2"]){
        belowData = @[@[@"", transStatus, @"3"]];
    }
    NSDictionary* dic = @{kTransType: transTypeTransfer,
            kBusinissType:transRecord,
            kAboveData: aboveData,
            kBelowData: belowData,
            @"status": status};
    [HPUIENG showScreen:SCR_TRANSACTION_FAIL params:dic];
}
- (void)showTransferInfo:(NSDictionary*)recordDic{
   
    NSString* status = [recordDic objectForKey:@"status"];
    
    if ([status isEqualToString:@"1"]) { //成功
        NSMutableDictionary* dic = [[NSMutableDictionary alloc]initWithDictionary:recordDic];
        [dic setObject:transRecord forKey:kBusinissType];
        [HPUIENG showScreen:SCR_SIGN_TRANSACTION params:dic];
        [dic release];
    }else{
        [self showFailDetail:recordDic];
    }
}
- (void)recordClick:(id)sender{
    isUpdate = NO;
    UIButton* button = (UIButton*)sender;
    NSInteger tagT = button.tag - kImageViewBgTag;
    if (dataArray.count >= tagT) {
        selectId = tagT;
        NSDictionary* recordDic = [dataArray objectAtIndex:(tagT)];
        NSString* imageUrl = [recordDic objectForKey:@"detailUrl"];
        if ([NSString isNilOrEmpty:imageUrl]) {
            isUpdate = YES;
            NSString* tempAppCode = [recordDic objectForKey:@"appcode"];
            
            if ([tempAppCode isEqualToString:@"1"]) {//卡卡转账
                [self showTransferInfo:recordDic];
            }else if ([appCode isEqualToString:@"3"]){//快速转账
//                [self showFastTransferInfo:recordDic];
            }
        }else{//签购单存在，获取签购单
            [self downloadImage:imageUrl];
        }
    }
}

-(void)dealloc{
    if (contentScrolView) {
        [contentScrolView release];
    }
    if (bottomView) {
        [bottomView release];
    }
    if (pageLabel) {
        [pageLabel release];
    }
    if (homePageButton) {
        [homePageButton release];
    }
    if (prevPageButton) {
        [prevPageButton release];
    }
    if (nextPageButton) {
        [nextPageButton release];
    }
    if (lastPageButton) {
        [lastPageButton release];
    }
    if (dateBgView) {
        [dateBgView release];
    }
    if (endDateView) {
        [endDateView release];
    }
    if (beginDateView) {
        [beginDateView release];
    }
    if (queryButton) {
        [queryButton release];
    }
    if (recordTableView) {
        [recordTableView release];
    }
    if (totalAmountLabel) {
        [totalAmountLabel release];
    }
    if (totalNumLabel) {
        [totalNumLabel release];
    }
    self.totalAmount = nil;
    self.totalNum = nil;
    if (dataArray) {
        [dataArray release];
    }
    self.appCode = nil;
    if (cellCountArray) {
        [cellCountArray release];
    }
    [super dealloc];
}
- (NSUInteger)heightWithLabel:(UILabel*)label{
    label.numberOfLines = 0;
    
    CGRect frame = label.frame;
    frame.size.height = 1000;
    label.frame = frame;
    
    [label sizeToFit];
    
    NSUInteger h = label.frame.size.height;
    
    if (h >= RecordCellHeight) {
        label.textAlignment = NSTextAlignmentLeft;
        h += 15;
    }else{
        h = RecordCellHeight;
        
    }
    frame.size.height = h;
    label.frame = frame;
    return h;
}
#pragma mark - table view delegate
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    if (cellCountArray) {
        NSArray* arr = [cellCountArray objectAtIndex:indexPath.section];
        if (arr && arr.count > 2 && (indexPath.row == 8)) {
            return ((NSNumber*)[arr objectAtIndex:2]).intValue;
        }
    }
    return RecordCellHeight;
}

#pragma mark - Table view data source

- (NSInteger) numberOfSectionsInTableView:(UITableView *)tableView{
    return dataArray.count;
}

- (NSInteger) tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    if ([appCode isEqualToString:@"1"]) {
        return RecordCellCountTransfer;
    }
    if (cellCountArray) {
        NSArray* array = [cellCountArray objectAtIndex:section];
        if (array) {
            NSNumber* count = [array objectAtIndex:0];
            if (section == (dataArray.count - 1)) {
                return count.intValue - 1;
            }
            return count.intValue;
        }
    }
    if (section == (dataArray.count - 1)) {
        return (RecordCellCount - 1);
    }
    return RecordCellCount;
}
- (NSArray*)cellForRowFast:(NSIndexPath*)indexPath{
    NSString* key = nil;
    NSString* value = nil;
    NSString* status = nil;
    
    NSDictionary* recordDic = [dataArray objectAtIndex:indexPath.section];
    switch (indexPath.row) {
        case 0:
            key = @"交易时间";
            value = [Utility getFormatDate:[recordDic objectForKey:@"transTime"]];
            break;
        case 1:
            key = @"交易状态";
            
            status = [recordDic objectForKey:@"status"];
            if ([status isEqualToString:@"1"]) {
                value = @"交易成功";
            }else if([status isEqualToString:@"0"]){
                value = @"交易失败";
            }else if([status isEqualToString:@"2"]){
                value = @"交易处理中";
            }else{
                value = @"";
            }
            break;
        case 2:
            key = @"交易流水号";
            value = [recordDic objectForKey:@"transSN"];
            break;
        case 3:
            key = @"转入卡号";
            value = [Utility encryptCardID:[recordDic objectForKey:@"cardNoIn"]];
            break;
        case 4:
            key = @"转出卡号";
            value = [Utility encryptCardID:[recordDic objectForKey:@"cardNoOut"]];
            break;
        case 5:
            key = @"转账金额";
            value = [Utility getFormatMoneyAndSymbol:[recordDic objectForKey:KEY_AMOUNT]];
            break;
        case 6:
            key = @"手续费用";
            value = [Utility getFormatMoneyAndSymbol:[recordDic objectForKey:KEY_FEE]];
            break;
        case 7:
            key = @"实付金额";
            value = [Utility getFormatMoneyAndSymbol:[recordDic objectForKey:KEY_TOTAL]];
//            color = kDarkGreenFontColor;
            break;
        case 8:
        {
            status = [recordDic objectForKey:@"status"];
            if ([status isEqualToString:@"0"]) {
                key = @"失败原因";
                value = [recordDic objectForKey:@"transStatus"];
            }
            else if ([status isEqualToString:@"2"]) {
                key = @"";
                value = [recordDic objectForKey:@"transStatus"];
                
            }else{
                key = @"";
                value = @"";
            }
            break;
        }
        case 9:
            key = @"";
            value = @"";
            break;
        default:
            return nil;
    }
    NSArray* array = @[key,value];
    return array;
}
- (NSArray*)cellForRow:(NSIndexPath*)indexPath{
    NSString* key = nil;
    NSString* value = nil;
    
    NSDictionary* recordDic = [dataArray objectAtIndex:indexPath.section];
    switch (indexPath.row) {
        case 0:
            key = @"交易时间";
            value = [Utility getFormatDate:[recordDic objectForKey:@"transTime"]];
            break;
        case 1:
            key = @"交易状态";
            
            NSString* status = [recordDic objectForKey:@"status"];
            if ([status isEqualToString:@"1"]) {
                value = @"交易成功";
            }else if([status isEqualToString:@"0"]){
                value = @"交易失败";
            }else if([status isEqualToString:@"2"]){
                value = @"交易处理中";
            }else{
                value = @"";
            }
            break;
        case 2:
            key = @"交易流水号";
            value = [recordDic objectForKey:@"transSN"];
            break;
        case 3:
            key = @"转出卡号";
            value = [Utility encryptCardID:[recordDic objectForKey:@"cardNoOut"]];
            break;
        case 4:
            key = @"实付金额";
            value = [Utility getFormatMoneyAndSymbol:[recordDic objectForKey:KEY_AMOUNT]];
            break;
        case 5:
            key = @"";
            value = @"";
            break;
        default:
            return nil;
    }
    NSArray* array = @[key,value];
    return array;
}

- (UITableViewCell *) tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    NSString* key = nil;
    NSString* value = nil;
    UIColor* color = kNormalFontColor;
    
    int h = 0;
    if (indexPath.row == 8) {
        if (cellCountArray) {
            NSArray* array = [cellCountArray objectAtIndex:indexPath.section];
            if (array && array.count > 2) {
                NSNumber* count = [array objectAtIndex:2];
                h = count.intValue;
            }
        }
    }
    NSArray* array = nil;
    if ([appCode isEqualToString:@"3"]) {
        array = [self cellForRowFast:indexPath];
        if (indexPath.row == 7) {
            color = kDarkGreenFontColor;
        }
    }else{
        array = [self cellForRow:indexPath];
        if (indexPath.row == 4) {
            color = kDarkGreenFontColor;
        }
    }
    key = [array objectAtIndex:0];
    value = [array objectAtIndex:1];
    
    static NSString *CellIdentifier = @"Cell";
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
    if (cell == nil) {
        cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier] autorelease];
        [cell setAutoresizesSubviews:NO];
        [cell setSelectionStyle:UITableViewCellSelectionStyleGray];
        cell.backgroundColor = [UIColor clearColor];
    }else{
        UILabel *desLabel = (UILabel *)[cell viewWithTag:kCellDesLabelTag];
        [desLabel removeFromSuperview];
        
        UILabel *nameLabel = (UILabel *)[cell viewWithTag:kCellValueLabelTag];
        [nameLabel removeFromSuperview];
    }
    
    UILabel *desLabel = nil;
    
    CGRect valueFrame = CGRectMake(10, 5, 280, 30);
    NSTextAlignment textAL = NSTextAlignmentLeft;
    if (![NSString isNilOrEmpty:key]) {
        desLabel = [[UILabel alloc] initWithFrame:CGRectMake(10, 5, 90, 30)];
        [desLabel setTag:kCellDesLabelTag];
        desLabel.text = key;
        [desLabel setBackgroundColor:[UIColor clearColor]];
        [desLabel setFont:kNormalFont];
        [desLabel setMinimumScaleFactor:14];
        [desLabel setTextColor:kNormalFontColor];
        [cell addSubview:desLabel];
        
        valueFrame = CGRectMake(100, 5, 190, 30);
        textAL = NSTextAlignmentRight;
    }
    
    
    UILabel *valueLabel = [[UILabel alloc] initWithFrame:valueFrame];
    [valueLabel setTextAlignment:textAL];
    [valueLabel setTag:kCellValueLabelTag];
    [valueLabel setBackgroundColor:[UIColor clearColor]];
    [valueLabel setFont:kNormalFont];
    valueLabel.text = value;
    [valueLabel setTextColor:color];
    
    if (h > 0) {
        valueLabel.textColor = [UIColor redColor];
        if (h > RecordCellHeight) {
            valueLabel.numberOfLines = 0;
            [valueLabel sizeToFit];
            valueLabel.textAlignment = NSTextAlignmentLeft;
            
            CGRect tempFrame = valueLabel.frame;
            tempFrame.size.height = h;
            valueLabel.frame = tempFrame;
            
            if (desLabel) {
                desLabel.frame = CGRectMake(10, 0, 90, h);
            }
            
        }
        
    }
    [cell addSubview:valueLabel];
    [valueLabel release];
    [desLabel release];
    
    return cell;

}
@end
